package com.bbx.flink.kafkaDemo;

import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.common.serialization.SimpleStringSchema;
import org.apache.flink.connector.kafka.source.KafkaSource;
import org.apache.flink.connector.kafka.source.enumerator.initializer.OffsetsInitializer;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

/**
 *
 * 从kafka 中读取数据
 *
 *
 */
public class SourceFromKafka {

    public static void main(String[] args) throws Exception {

        //执行环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        //创建source
        KafkaSource<String> source = KafkaSource.<String>builder().
                setBootstrapServers("master11.bbx.com:9092,worker12.bbx.com:9092,worker13.bbx.com:9092")
                .setGroupId("bbx-flink-groupID")
                .setTopics("bbx-flink-topic")
                //指定消费位置
                .setStartingOffsets(OffsetsInitializer.latest())
                .setValueOnlyDeserializer(new SimpleStringSchema())
                .build();

        //指定source
        DataStreamSource<String> kafkaSource = env.fromSource(source, WatermarkStrategy.noWatermarks(), "kafka-source");

        //sink
        kafkaSource.print("kafka-source");

        //执行
        env.execute("kafka-source-flink");

    }

}
